<template>
  <div class="patient-number-date">
    <div class="patient-number-date-inside">
      <div class="header">
        <div class="left item">
          <img
              alt="左侧"
              src="@/components/assets/images/design/1-12.webp"
          />
        </div>
        <div class="middle item">请点击下方卡片选择预约医生</div>
        <div class="right item">
          <img
              alt="右侧"
              src="@/components/assets/images/design/1-12.webp"
          />
        </div>
      </div>
      <el-row class="patient-number-area">
        <template v-if="true">
          <template v-if="DrScheduleList.state">
            <template v-for="item in DrScheduleInfo">
              <el-col :span="24" v-if="true">
                <div class="patient-number-item" @click="handleClick(item)">
                  <div class="first">
                    <div class="name">{{ item?.doctName || item?.deptName}}</div>
                    <div class="date">{{ item?.medDate }} {{ parseInt(item?.medAmPm) === 1 ? '上午' : '下午' }}</div>
                  </div>
                  <div class="second">
                    <div class="price">¥{{ (item.regfee) / 100 || '0.0' }}</div>
                    <template v-if="!!(item?.restnum)">
                      <div class="remaining">剩余 {{ (item?.restnum) }}</div>
                    </template>
                    <template v-else>
                      <div class="remaining empty">号源用尽</div>
                    </template>
                  </div>
                </div>
              </el-col>
            </template>
          </template>
          <template v-else>
            <el-empty description="暂无数据"></el-empty>
          </template>
        </template>

      </el-row>
    </div>
  </div>
</template>

<script>

import {buildParentApi} from "@/utils/designer/util";
import {v4 as uuid} from "uuid";
import {Toast} from 'vant';

let buildParent = buildParentApi();
export default {
  name: 'PatientNumberDoctor',

  componentName: "PatientNumberDoctor",

  components: {
    Toast
  },

  data() {
    return {}
  },

  computed: {

    // 医生排班数据处理
    DrScheduleInfo(){
      let {state, result} = this.DrScheduleList;
      if(state){
        let isArray = this.$lodash.isArray(result);
        if(isArray){
          return result
        }else{
          return [{...result}]
        }
      }
    },


    // 医生排班信息
    DrScheduleList() {
      return this.$store.getters['DRCbiz/getDrScheduleList']
    },

    PTypeInfo() {
      return this.$store.getters['DRCbiz/getOPTypeInfo']
    },

    bizInfo() {
      return this.$store.getters['DRCbiz/getBizInfo']
    },

    deptInfo() {
      return this.$store.getters['DRCbiz/getDeptInfo']
    },

    cardTypeInfo() {
      return this.$store.getters['DRCbiz/getCardTypeInfo']
    },

    patientInfo() {
      return this.$store.getters['DRCbiz/getPatientInfo']
    }

  },

  mounted() {
  },

  methods: {
    handleClick(row) {
      let bizInfo = (this.bizInfo);
      let bizType = parseInt(bizInfo?.id);
      if (!!(row?.restnum)) {
        switch (bizType) {
            // 预约--普通号&专家号
          case 1:
            this.querySourceList(row);
            break;
            // 挂号--普通号&专家号
          case 2:
            this.PreAppointReg(row);
            break;
        }
      } else {
        Toast({
          message: `号源用尽，请选择其他号源`,
          position: 'bottom',
          type: 'fail',
          forbidClick: true,
          duration: 3000
        });
      }
    },

    // 预结算业务
    PreAppointReg(row) {
      const self = this;
      let PTypeInfo = (this.PTypeInfo);
      let bizInfo = (this.bizInfo);
      let patientInfo = (this.patientInfo);
      let cardTypeInfo = (this.cardTypeInfo);
      let hospAreaInfo = (this.hospAreaInfo);
      // 存储选中的排班信息
      this.$store.dispatch('DRCbiz/setCurrentScheduleInfo', row);
      let params = Object();
      let paramInfo = Object();
      // 2专家(医生排班--> 预结算信息-->结算结果)&1普通(号源排班--> 预结算信息-->结果)
      let devInfo = localStorage.getItem('deviceInfo');
      Reflect.set(paramInfo, 'deviceIp', (devInfo?.screenIp));
      Reflect.set(paramInfo, 'flowId', uuid());
      Reflect.set(paramInfo, 'timestamp', self.$day().valueOf());
      Reflect.set(paramInfo, 'terminalNo', (devInfo?.screenId));
      Reflect.set(paramInfo, 'method', 'preAppointReg');
      Reflect.set(paramInfo, 'hospCode', (hospAreaInfo?.id) || 1);
      // 电子医保凭证
      if (parseInt(cardTypeInfo?.id) === 29) {
        Reflect.set(paramInfo, 'cardNo', (cardTypeInfo?.cardInfo));
        Reflect.set(paramInfo, 'cardHardInfo', (cardTypeInfo?.cardInfo));
      } else {
        Reflect.set(paramInfo, 'cardNo', (cardTypeInfo?.cardNo));
        Reflect.set(paramInfo, 'cardHardInfo', (cardTypeInfo?.cardInfo));
      }
      Reflect.set(paramInfo, 'cardType', (cardTypeInfo.id));
      Reflect.set(paramInfo, 'regMode', (bizInfo.id));
      Reflect.set(paramInfo, 'regType', (PTypeInfo.level));
      Reflect.set(paramInfo, 'medDate', this.$day().format('YYYY-MM-DD'));
      Reflect.set(paramInfo, 'patientId', (patientInfo?.result?.patientId));

      Reflect.set(paramInfo, 'deptCode', (row?.deptCode));
      Reflect.set(paramInfo, 'scheduleId', (row?.scheduleId));
      Reflect.set(paramInfo, 'doctCode', (row?.doctCode));
      Reflect.set(paramInfo, 'medAmPm', (row?.medAmPm) || 1);

      // 数据需要确认
      Reflect.set(paramInfo, 'cash', (row?.regAmount));
      Reflect.set(paramInfo, 'ybCardNo', null);
      Reflect.set(paramInfo, 'isZj', 0);
      Reflect.set(paramInfo, 'TOKEN', null);
      Reflect.set(paramInfo, 'deviceVersion', uuid());
      Reflect.set(paramInfo, 'deviceInfo', uuid());
      Reflect.set(paramInfo, 'operId', uuid());
      Reflect.set(paramInfo, 'sourceCode', 2);
      Reflect.set(paramInfo, 'tradeTime', this.$day().format('YYYY-MM-DD HH:mm:ss'));
      Reflect.set(params, 'Request', paramInfo);
      let paramsInfo = this.$x2js.js2xml(params);
      buildParent.executeParentMethod('axiosRep', paramsInfo, 'preAppointReg');
    },

    // 时间间隙
    querySourceList(row) {
      console.log('querySourceList')
      const self = this;
      let params = Object();
      let paramInfo = Object();
      let hospAreaInfo = (this.hospAreaInfo);
      let bizInfo = (this.bizInfo);
      let PTypeInfo = (this.PTypeInfo);
      this.$store.dispatch('DRCbiz/setCurrentScheduleInfo',row);
      let currDateInfo = this.$store.getters['DRCbiz/getDateInfo'];
      // 2专家(医生排班--> 预结算信息-->结算结果)&1普通(号源排班--> 预结算信息-->结果)
      let devInfo = localStorage.getItem('deviceInfo');
      Reflect.set(paramInfo, 'deviceIp', (devInfo?.screenIp));
      Reflect.set(paramInfo, 'flowId', uuid());
      Reflect.set(paramInfo, 'timestamp', self.$day().valueOf());
      Reflect.set(paramInfo, 'terminalNo', (devInfo?.screenId));
      Reflect.set(paramInfo, 'method', 'querySourceList');
      Reflect.set(paramInfo, 'hospCode', (hospAreaInfo?.id) || 1);

      Reflect.set(paramInfo, 'regMode', (bizInfo.id));
      Reflect.set(paramInfo, 'regType', (PTypeInfo.level));
      Reflect.set(paramInfo, 'scheduleId', (row?.scheduleId));
      Reflect.set(paramInfo, 'medAmPm', (row?.medAmPm) || 1);
      if (parseInt(bizInfo?.id) === 1) {
        Reflect.set(paramInfo, 'medDate', currDateInfo?.dateInfo || this.$day().format('YYYY-MM-DD'));
      } else {
        Reflect.set(paramInfo, 'medDate', this.$day().format('YYYY-MM-DD'));
      }
      Reflect.set(paramInfo, 'doctCode', (row?.doctCode));
      Reflect.set(params, 'Request', paramInfo);
      let paramsInfo = this.$x2js.js2xml(params);
      buildParent.executeParentMethod('axiosRep', paramsInfo, 'querySourceList');
    }
  },
}

</script>

<style scoped lang="scss">
@import "assets/style";
</style>